import 'package:flutter/material.dart';
const cityNames = {
  '苏州':['姑苏区','工业园区','虎丘区','相城区','吴中区','吴江区','昆山','常熟','张家港','太仓'],
  '徐州':['云龙区','鼓楼区','泉山区','铜山区','贾汪区','邳州市','新沂市','丰县','沛县','睢宁县']
};

class ExpansionTilePage extends StatefulWidget {
  const ExpansionTilePage({super.key});

  @override
  State<ExpansionTilePage> createState() => _ExpansionTilePageState();
}

class _ExpansionTilePageState extends State<ExpansionTilePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('可展开的列表'),),
      body: ListView(
        children: _buildList(),
      ),
    );
  }

  List<Widget> _buildList(){
    List<Widget> widgetList = [];
    for(var key in cityNames.keys){// 构建最外层列表
      widgetList.add(_buildFatherItem(key,cityNames[key]!));
    }
    return widgetList;
  }

  // 构建最外层列表的一个元素，同时构建其下的子元素
  Widget _buildFatherItem(String city, List<String> subCities){
    return ExpansionTile(
      title: Text(city, style: const TextStyle(fontSize: 20, color: Colors.black54),),
      children: subCities.map((subCity) => _buildSub(subCity)).toList(),
    );
  }

  // 构建第二层子元素，返回给其父元素
  Widget _buildSub(String subCity){
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        height: 50,
        margin: const EdgeInsets.only(bottom: 5),
        decoration: const BoxDecoration(color: Colors.amber),
        child: Text(subCity),
      )
    );
  }
}
